<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 头部搜索区域 -->
      <el-row :gutter='20'>
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="searchGoods()">
            <el-button slot="append" icon="el-icon-search" @click="searchGoods()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button type="primary" @click="addGoodsData()">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表单区域 -->
      <el-table :data="goodslistData" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column class="text" prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="120px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="200px">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteData(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 删除商品-弹出层区域 -->
    <el-dialog title="提示" :visible.sync="deletedialogVisible" width="30%">
      <span>确定删除此商品？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deletedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteGoodsSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单商品列表
      goodslistData: [],
      // 查询参数列表
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品总数
      total: 0,
      // 删除商品窗口是否显示
      deletedialogVisible: false,
      deleteID: ''
    }
  },
  created () {
    this.getGoodsData()
  },
  methods: {
    // 1、获取商品数据列表
    async getGoodsData () {
      const {
        data: res
      } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.goodslistData = res.data.goods
      this.total = res.data.total
    },
    // 监听pagesize改变的事件
    handleSizeChange (newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getGoodsData()
    },
    // 监听页码pagenum的改变
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsData()
    },
    // 2、实现搜索商品功能
    searchGoods () {
      console.log(this.queryInfo.query)
      this.getGoodsData()
    },
    // 3、实现添加商品页面-使用前端路由跳转过去
    addGoodsData () {
      this.$router.push('/goods/add')
    },
    // 4、删除商品功能
    deleteData (goodsData) {
      console.log(goodsData)
      this.deletedialogVisible = true
      this.deleteID = goodsData.goods_id
    },
    // 点击确定按钮，删除商品，调用接口
    async deleteGoodsSubmit () {
      const { data: res } = await this.$http.delete('goods/' + this.deleteID)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.getGoodsData()
      this.deletedialogVisible = false
      this.$message.success('删除成功')
    }
  }
}

</script>

<style lang="less" scoped>
  .el-table {
    margin: 15px 0;
  }

</style>
